package com.yuan.support

import android.graphics.Bitmap
import id.zelory.compressor.Compressor
import id.zelory.compressor.constraint.format
import id.zelory.compressor.constraint.quality
import id.zelory.compressor.constraint.resolution
import id.zelory.compressor.constraint.size
import kotlinx.coroutines.*
import java.io.File
import kotlin.coroutines.CoroutineContext

/**
 * create by water.yuan
 * on 2021-07-21
 *
 * Kotlin 协程 工具类
 */
class KtUtils : CoroutineScope {
    // 使用job来管理你的SomethingWithLifecycle的所有子协程
    private val job = Job()
    override val coroutineContext: CoroutineContext
        get() = Dispatchers.Main + job

    fun destory() {//退出取消
        job.cancel()
    }

    fun other(imageFile: File): File? {//退出取消

        //创建主线程分发处理Scope
        MainScope().launch {
            var compressedImage = Compressor.compress(ContextUtils.getContextByReflection(), imageFile) {
                resolution(1280, 720)
                quality(80)
                format(Bitmap.CompressFormat.JPEG)
                size(300 * 1024) // 300KB
            }
        }

        return null
    }
}
